package com.leizi.igym.controller.user;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.leizi.igym.constant.CourseConstant;
import com.leizi.igym.domain.Course;
import com.leizi.igym.dto.CourseAddDTO;
import com.leizi.igym.dto.CourseDTO;
import com.leizi.igym.dto.CoursePageQueryDTO;
import com.leizi.igym.enums.FileUploadBizEnum;
import com.leizi.igym.enums.ReturnCodeEnum;
import com.leizi.igym.exception.BusinessException;
import com.leizi.igym.manager.CosManager;
import com.leizi.igym.resp.PageResult;
import com.leizi.igym.resp.Result;
import com.leizi.igym.service.CourseService;
import com.leizi.igym.utils.COSUtils;
import com.leizi.igym.utils.ThrowUtils;
import com.leizi.igym.vo.CourseGoodsVO;
import com.leizi.igym.vo.CourseVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * @Author: leizi0110
 * @CreateTime: 2024-04-05  17:48
 * @Description:
 */

@RestController("userCourseController")
@RequestMapping("/user/course")
@Tag(name = "用户端-课程相关接口")
@Slf4j
public class CourseController {

    @Resource
    private CourseService courseService;

    @Resource
    private CosManager cosManager;


    @Operation(summary = "用户端：分页查询课程信息")
    @GetMapping("page")
    public Result<PageResult> getTrainerPage(CoursePageQueryDTO dto) {
        log.info("用户端正在分页查询课程信息，查询参数为：{}", dto);
        ThrowUtils.throwIf(Objects.isNull(dto), ReturnCodeEnum.PARAMS_ERROR);

        long current = dto.getCurrent();
        long size = dto.getPageSize();
        // 限制爬虫
        ThrowUtils.throwIf(size > 50 || current <= 0 || size <= 0, ReturnCodeEnum.PARAMS_ERROR);
        Page<CourseGoodsVO> page = courseService.getCourseGoodsVOPage(dto);
        return page == null ? Result.ok(PageResult.builder()
                .records(Collections.emptyList())
                .total(0)
                .build()) : Result.ok(PageResult.builder()
                .records(page.getRecords())
                .total(page.getTotal())
                .build());
    }

    @Operation(summary = "用户端：根据id查询课程信息")
    @GetMapping("{id}")
    public Result<CourseVO> getById(@PathVariable("id") Long id) {
        log.info("用户端正在根据id查询课程信息，id为：{}", id);
        ThrowUtils.throwIf(Objects.isNull(id) || id <= 0,
                ReturnCodeEnum.PARAMS_ERROR);

        CourseVO courseVO = courseService.getVOById(id);
        if (Objects.isNull(courseVO)) {
            throw new BusinessException(ReturnCodeEnum.NOT_FOUND_ERROR, "该教练信息不存在！");
        }

        return Result.ok(courseVO);
    }




}
